package com.hz.ys_stock.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.MyAction.BaseAction;
import com.hz.framework.MyInterFace.SystemInterFace;
import com.hz.framework.dao.CommonDBDao;
import com.hz.framework.dao.CommonDao;
import com.hz.framework.util.DateUtil;
import com.hz.framework.util.SqlParameter;
import com.hz.ys_stock.bean.PresPurchase;
import com.hz.ys_stock.dbsql.StockSql;
import com.hz.ys_system.dao.QueryDao;

/**
 * *****************************************************************************
 *               Copyright(c) 2010 by 哈尔滨华泽数码科技有限公司.
 *                       All rights reserved.
 *******************************************************************************
 *     File Name         :  com.hz.ys_stock.dao.PurchaseDao.java
 *     Description(说明)	:  采购单信息管理
 * -----------------------------------------------------------------------------
 *     No.        Date              Revised by           Description	  
 *     1		Jun 8, 2011				韩飞	              Created
 *****************************************************************************
 */
public class PurchaseDao implements SystemInterFace{
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询采购单信息
	 */
	public String SelPurchase(HttpServletRequest request){
		StringBuilder sbSQL = new StringBuilder();
		sbSQL.append(StockSql.SEL_PURCHASE);
		
		sbSQL.append(new QueryDao().QueryInfo(request , new SqlParameter("purchaseid","r.suppliername","buyer") , "like") );
		//时间条件查询
		sbSQL.append(new QueryDao().QueryDateInfo(request,"orderdate","orderbegin","orderend",true));
		//时间条件查询
		sbSQL.append(new QueryDao().QueryDateInfo(request,"deliverydate","deliverybegin","deliveryend",false));
		
		sbSQL.append(" order by purchaseid desc");
		System.out.println(sbSQL.toString());
		return sbSQL.toString();
	}
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param pur
	 * @param request
	 * @throws Exception 
	 * 方法功能:  保存采购单信息 和采购物料信息
	 */
	public void DoSavePurchase(PresPurchase pur, HttpServletRequest request) throws Exception {
		CommonDao dbo= new CommonDao();
		if(pur!=null){
			if("到货".equals(pur.getProgress())){
//				如果到货 更新库存数量
				pur.setDeliverydate(DateUtil.getTime());
			}
			pur.setCreatetime(DateUtil.getDateTime());
			if(dbo.insertinfor(pur)){
				DoSaveBuymak(pur,request,dbo);
			}
		}
	}
	/**
	 * @author 韩飞
	 * @time Nov 22, 2011	
	 * @param pur
	 * @param request
	 * @throws Exception 
	 * 方法功能:  保存采购单信息 和采购物料信息
	 */
	public boolean DoSaveBuymak(PresPurchase pur, HttpServletRequest request,CommonDao dbo) throws Exception {
		boolean ins = false;
		if(pur!=null){
			BaseAction ba = new BaseAction();
			String[] fields = {"purchaseid","goodsid","sumgoods","price","summoney","createtime","remark","buyprice","buynum","buyunit"};
			String[] field = {"goodsid","sumgoods","price","summoney","remark","buyprice","buynum","buyunit"};
			//	获得页面数据
			List list = ba.dogetList(request,field);
			if(list!=null){
				List inssql = new ArrayList();
				for(int i = 0;i<list.size();i++){
					Map map = (Map)list.get(i);
					map.put("purchaseid", pur.getPurchaseid());
					map.put("createtime", pur.getCreatetime());
					inssql.add(dbo.doInsertSQL(map, fields, "pres_buymak"));
				}
				ins = dbo.insorupdArray(inssql);
//					如果选择到货，直接标记到库存中
				if("到货".equals(pur.getProgress())){
//					如果到货 更新库存数量
					new StockDao().Stock_Label(list);
				}
				request.setAttribute("msg", ins);
			}
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 根据 purchaseid 查询 对应采购单信息 和 采购物料信息
	 */
	public void SelPurchase_Info(HttpServletRequest request) throws Exception {
		String purchaseid = CommonDao.isnull(request.getParameter("pid"));
		CommonDao dbd= new CommonDao();
		request.setAttribute("purchaseinfo", dbd.queryObjectByPS(StockSql.UPD_PURCHASE_INFO, new SqlParameter(purchaseid),null));
		request.setAttribute("makingsinfo", dbd.queryListByPS(StockSql.UPD_MAKINGS_INFO, new SqlParameter(purchaseid),null));
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @param request
	 * @return 
	 * 方法功能: 删除 采购单信息 及对应的 采购物料信息
	 */
	public boolean Del_Purchase(HttpServletRequest request) {
		boolean ins = false;
		String pid = CommonDao.isnull(request.getParameter("pid"));
		CommonDBDao dbo= new CommonDBDao();
		if(dbo.executeSQL(StockSql.DEL_PURCHASE_INFO, new SqlParameter(pid))){
			ins = dbo.executeSQL(StockSql.DEL_BUYMAK_INFO, new SqlParameter(pid));
		}
		return ins;
	}
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能: 修改采购单信息 及对应的 采购物料信息
	 */
	public void UpdPurchase(PresPurchase pur , HttpServletRequest request) throws Exception{
		boolean ins = false;
		CommonDao dbo= new CommonDao();
		if(dbo.updateinfor(pur)){
			dbo.executeSQL(StockSql.DEL_BUYMAK_INFO, new SqlParameter(pur.getPurchaseid()));
			ins = DoSaveBuymak(pur,request,dbo);
		}
	}
	
	/**
	 * @author 韩飞
	 * @time Nov 16, 2011	
	 * @return 
	 * 方法功能:查询所有人员
	 */
	public List SelBuyer(){
		CommonDBDao dbo= new CommonDBDao();
		return dbo.DBjsonList(StockSql.SEL_BUYER);
	}
	
	
}
